local extension = Package("rfenghou_newdyn")
extension.extensionName = "aaa_fenghou"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["rfenghou_newdyn"] = "新新",
  ["newdyn"] = "新",
}

local liuxiu = General:new(extension, "rfenghou__liuxiu", "newdyn", 4)

local isLightningDamage = function(data)
  if data.skillName then
    if data.skillName == "lightning_skill" or Fk:translate(data.skillName, "zh_CN") == "闪电" then
      return true
    end
  end
  if data.from then
    if Fk:translate(data.from.general, "zh_CN") == "闪电" or Fk:translate(data.from.deputyGeneral, "zh_CN") == "闪电" then
      return true
    end
  end
  -- 非常愚蠢的假闪电判定，令我检索困难
  local room = data.to.room---@type Room
  local currentEvent = room.logic:getCurrentEvent()
  local skillEvent = currentEvent:findParent(GameEvent.SkillEffect)
  if not skillEvent then return false end
  local id = currentEvent.id - 1
  local lastEvent
  while id > skillEvent.id do
    local e = room.logic.all_game_events[id]
    if not e then break end
    if e.parent == skillEvent then
      lastEvent = e
      break
    end
    id = id - 1
  end
  -- 查找本伤害事件上一个事件是否为闪电判定
  if lastEvent and lastEvent.event == GameEvent.Judge and lastEvent.data[1].reason == "lightning" then
    return true
  end
  return false
end

local yiju = fk.CreateTriggerSkill{
  name = "rfenghou__yiju",
  anim_type = "drawcard",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and isLightningDamage(data)
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(3, self.name)
  end,
}
liuxiu:addSkill(yiju)

local daitian = fk.CreateActiveSkill{
  name = "rfenghou__daitian",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  prompt = "#rfenghou__daitian",
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    local to = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and (to:getMark("rfenghou__daitian_tar") ~= 0
    or table.every(Fk:currentRoom().alive_players, function (p)
      return p == to or p.hp <= to.hp
    end))
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    room:damage { from = player, to = to, damage = 1, skillName = self.name }
  end,
}

local daitian_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__daitian_trigger",
  anim_type = "drawcard",
  events = {fk.Deathed},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(daitian) and data.damage and data.damage.skillName == "rfenghou__daitian"
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, daitian.name, nil, "#rfenghou__daitian-changeskill")
  end,
  on_use = function (self, event, target, player, data)
    player:setSkillUseHistory("rfenghou__shandian", player:usedSkillTimes("rfenghou__daitian", Player.HistoryPhase), Player.HistoryPhase)
    player.room:handleAddLoseSkills(player, "-rfenghou__daitian|rfenghou__shandian", nil, false, true)
  end,

  refresh_events = {fk.Damaged},
  can_refresh = function(self, event, target, player, data)
    return target == player and data.skillName == "rfenghou__daitian" and player:getMark("rfenghou__daitian_tar") == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rfenghou__daitian_tar", 1)
  end,
}
daitian:addRelatedSkill(daitian_trigger)

-- 两个同时机触发技，so写成俩技能
local daitian_changehero = fk.CreateTriggerSkill{
  name = "#daitian_changehero",
  mute = true,
  events = {fk.Deathed},
  can_trigger = function(self, event, target, player, data)
    if player.general == "rfenghou__lightningguy" or player.deputyGeneral == "rfenghou__lightningguy" then return false end
    if player:hasSkill(daitian) or player:hasSkill("rfenghou__shandian") then
      if data.damage then
        local damageEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.Damage)
        if damageEvent then
          return isLightningDamage(damageEvent.data[1])
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, daitian.name, nil, "#rfenghou__daitian-changehero")
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerGeneral(player, "rfenghou__lightningguy")
    player:broadcastSkillInvoke("rfenghou__shandian")
    player.room:notifySkillInvoked(player, "rfenghou__shandian", "big")
  end,
}
daitian:addRelatedSkill(daitian_changehero)

liuxiu:addSkill(daitian)

local rfenghou__shandian = fk.CreateActiveSkill{
  name = "rfenghou__shandian",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  prompt = "#rfenghou__daitian",
  card_filter = Util.FalseFunc,
  target_filter = daitian.targetFilter,
  can_use = daitian.canUse,
  on_use = daitian.onUse,
}
Fk:addSkill(rfenghou__shandian)

local lightningguy = General:new(extension, "rfenghou__lightningguy", "newdyn", 4)
lightningguy.total_hidden = true
lightningguy.hidden = true

Fk:loadTranslationTable{
  ["rfenghou__liuxiu"] = "刘秀",
  ["#rfenghou__liuxiu"] = "",
  ["designer:rfenghou__liuxiu"] = "zzcclll朱苦力",
  ["illustrator:rfenghou__liuxiu"] = "",

  ["rfenghou__yiju"] = "义举",
  [":rfenghou__yiju"] = "当“闪电”造成伤害后，你可以摸三张牌。",

  ["rfenghou__daitian"] = "代天",
  [":rfenghou__daitian"] = "出牌阶段限一次，你可以对体力值最高，或受到过“代天”伤害的角色造成1点伤害。因“代天”造成击杀后，你可以将此技能名改为“闪电”；因“闪电”造成击杀后，你可以将武将名改为“闪电”。",
  ["#rfenghou__daitian"] = "你可以对体力值最高，或受到过“代天”伤害的角色造成1点伤害",
  ["#rfenghou__daitian_trigger"] = "代天",
  ["#rfenghou__daitian-changeskill"] = "你可以将技能“代天”改名为“闪电”",

  ["rfenghou__shandian"] = "闪电",
  [":rfenghou__shandian"] = "出牌阶段限一次，你可以对体力值唯一最高，或受到过“代天”伤害的角色造成1点伤害。因“闪电”造成击杀后，你可以将武将名改为“闪电”。",
  ["#daitian_changehero"] = "代天变身",
  ["#rfenghou__daitian-changehero"] = "你可以将变身为“闪电”！",
  ["rfenghou__lightningguy"] = "闪电",

  ["$rfenghou__shandian1"] = "我当要替天行道！",
  ["$rfenghou__shandian2"] = "我就是天意啊",
}

return extension
